package com.paomi.client.fragment.information.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.newsDetail.NewsDetailActivity;
import com.paomi.client.activity.newsDetail.model.NewsListJson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class InformaticaAdapter extends RecyclerView.Adapter<InformaticaAdapter.ViewHolder> {
    Context context;
    List<NewsListJson> list = new ArrayList<>();

    public InformaticaAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<NewsListJson> data) {
        this.list = data;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_classify_informatica, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        NewsListJson newsListJson=list.get(position);
        GlideApp.with(context).load(newsListJson.getImg()).placeholder(R.mipmap.img_default_msg).into(holder.infoIv);
        holder.infoTitle.setText(newsListJson.getTitle());
        holder.infoDate.setText(newsListJson.getSource());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,NewsDetailActivity.class);
                intent.putExtra("newId",""+newsListJson.getId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.info_iv)
        ImageView infoIv;
        @BindView(R.id.info_title)
        TextView infoTitle;
        @BindView(R.id.info_date)
        TextView infoDate;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
